package com.gf.event.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.gf.base.dao.impl.BaseDaoImpl;
import com.gf.base.type.EventType;
import com.gf.base.type.OperationType;
import com.gf.common.bean.Page;
import com.gf.event.dao.EventDao;
import com.gf.event.mapper.EventMapper;
import com.gf.event.model.Event;

@Repository("eventDao")
public class EventDaoImpl extends BaseDaoImpl implements EventDao {

	/**
	 * insert into event (userId,
	 * userName,operation,title,portrait,targetId,eventType,createTime) values
	 * (?,?,?,?,?,?,?,current_timestamp)
	 */
	@Value("${eventDao.insertEventSql}")
	private String insertEventSql;

	/**
	 * select * from event where userId=? order by createTime desc
	 */
	@Value("${eventDao.queryEventsByUserIdSql}")
	private String queryEventsByUserIdSql;

	/**
	 * select * from event order by createTime desc
	 */
	@Value("${eventDao.queryAllEventSql}")
	private String queryAllEventSql;

	/**
	 * update event set portrait=? where userId=?
	 */
	@Value("${eventDao.updatePortraitSql}")
	private String updatePortraitSql;

	@Value("${eventDao.queryRecentEventSql}")
	private String queryRecentEventSql;

	/**
	 * update event set activity='DELETED' where targetId=? and eventType=?
	 */
	@Value("${eventDao.deleteEventSql}")
	private String deleteEventSql;

	@Override
	public long insertEvent(long userId, String userName, String operation,
			String title, String portrait, long targetId, EventType eventType) {
		return this.insertWithIdReturn(insertEventSql, userId, userName,
				operation, title, portrait, targetId, eventType.toString());
	}

	@Override
	public List<Event> queryEventsByUserId(long userId, Page page) {

		return this.getPaginationResult(queryEventsByUserIdSql, page,
				EventMapper.getMapper(), userId);
	}

	@Override
	public List<Event> queryEventsOfIdols(long userId, List<Long> idols,
			Page page) {
		if (idols.size() == 0) {
			return new ArrayList<Event>();
		}
		String inSet = "" + idols.get(0);
		for (int i = 1; i < idols.size(); i++) {
			inSet += ("," + idols.get(i));
		}
		// 用in的时候 用注入有问题，只会查找出第一个，所有用手动拼凑
		String queryEventsOfIdolsSql = "select * from event where userId in ("
				+ inSet + ") order by createTime desc";
		return this.getPaginationResult(queryEventsOfIdolsSql, page,
				EventMapper.getMapper());
	}

	@Override
	public List<Event> queryAllEvent(Page page) {

		return getPaginationResult(queryAllEventSql, page,
				EventMapper.getMapper());
	}

	@Override
	public int updatePortrait(long userId, String newPortrait) {

		return jdbcTemplate.update(updatePortraitSql, newPortrait, userId);
	}

	@Override
	public List<Event> queryRecentEvent(Page page) {
		return getPaginationResult(queryRecentEventSql, page,
				EventMapper.getMapper(), OperationType.CAN_JIA);
	}

	@Override
	public boolean deleteEvent(long targetId, EventType eventType) {

		return jdbcTemplate.update(deleteEventSql, targetId,
				eventType.toString()) > 0 ? true : false;
	}
}
